package com.xiu.travel.util;


import com.xiu.travel.util.api.CommonResult;
import com.alibaba.fastjson.JSON;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

public class WebUtils
{
    /**
     * 将字符串渲染到客户端
     *
     * @param r 渲染对象
     * @return null
     */
    public static String renderString(CommonResult<?> r) {
        HttpServletResponse response = ServletUtils.getResponse();
        try
        {
            Objects.requireNonNull(response).setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(JSON.toJSONString(r));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

